﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program1

    {

        public static void xs(int[,] a)

        {

            for (int i = 0; i < 4; i++)

            {

                for (int j = 0; j < 4; j++)

                {

                    Console.Write(a[i, j].ToString().PadRight(6, ' '));

                }

                Console.WriteLine();

                Console.WriteLine();

            }

        }

        public static void start(int[,] a)

        {

            Random random = new Random();

            int x = 0;

            int y = 0;

            for (int i = 0; i < 2;)

            {

                x = random.Next(0, 4);

                y = random.Next(0, 4);

                if (a[x, y] == 0)

                {

                    a[x, y] = 2;

                    i++;

                }

            }

        }

        public static void sc(int[,] a)

        {

            Random random = new Random();

            int x = 0;

            int y = 0;

            for (int i = 0; i < 1;)

            {

                x = random.Next(0, 4);

                y = random.Next(0, 4);

                if (a[x, y] == 0)

                {

                    a[x, y] = random.Next(1, 3) * 2;

                    i++;

                }

            }

        }

        static void Main(string[] args)

        {

            int[,] a = new int[4, 4]

            {

                {0,0,0,0},

                {0,0,0,0},

                {0,0,0,0},

                {0,0,0,0}

            };

            start(a);

            Console.WriteLine();

            for (; ; )

            {

                int ind = 0;

                xs(a);

                ConsoleKeyInfo aj = Console.ReadKey();

                if (aj.Key.ToString() == "UpArrow")

                {



                    for (int w = 0; w < 4; w++)

                    {

                        for (int q = 0; q < 3; q++)

                        {

                            if (a[q, w] == a[q + 1, w])

                            {

                                a[q, w] = a[q, w] + a[q + 1, w];

                                a[q + 1, w] = 0;

                            }

                        }



                    }

                    int i, j, k;

                    for (j = 0; j < 4; j++)

                    {

                        for (i = 1; i < 4; i++)

                        {

                            k = i;

                            while (k - 1 >= 0 && a[k - 1, j] == 0)

                            {//上面的那个为空   

                                int temp;

                                temp = a[k, j];

                                a[k, j] = a[k - 1, j];

                                a[k - 1, j] = temp;

                                k--;

                            }

                        }

                    }

                }

                if (aj.Key.ToString() == "DownArrow")

                {



                    for (int w = 0; w < 4; w++)

                    {

                        for (int q = 3; q >= 1; q--)

                        {

                            if (a[q, w] == a[q - 1, w])

                            {

                                a[q, w] = a[q, w] + a[q - 1, w];

                                a[q - 1, w] = 0;

                            }

                        }



                    }

                    int i, j, k;

                    for (j = 0; j < 4; j++)

                    {

                        for (i = 2; i >= 0; i--)

                        {

                            k = i;

                            while (k + 1 <= 3 && a[k + 1, j] == 0)

                            {//上面的那个为空   

                                int temp;

                                temp = a[k, j];

                                a[k, j] = a[k + 1, j];

                                a[k + 1, j] = temp;

                                k++;

                            }

                        }

                    }

                }

                if (aj.Key.ToString() == "LeftArrow")

                {



                    for (int q = 0; q < 4; q++)

                    {

                        for (int w = 0; w < 3; w++)

                        {

                            if (a[q, w] == a[q, w + 1])

                            {

                                a[q, w] = a[q, w] + a[q, w + 1];

                                a[q, w + 1] = 0;

                            }

                        }



                    }

                    int i, j, k;

                    for (i = 0; i < 4; i++)

                    {

                        for (j = 1; j < 4; j++)

                        {

                            k = j;

                            while (k - 1 >= 0 && a[i, k - 1] == 0)

                            {//上面的那个为空   

                                int temp;

                                temp = a[i, k];

                                a[i, k] = a[i, k - 1];

                                a[i, k - 1] = temp;

                                k--;

                            }

                        }

                    }

                }

                if (aj.Key.ToString() == "RightArrow")

                {



                    for (int q = 0; q < 4; q++)

                    {

                        for (int w = 3; w >= 1; w--)

                        {

                            if (a[q, w] == a[q, w - 1])

                            {

                                a[q, w] = a[q, w] + a[q, w - 1];

                                a[q, w - 1] = 0;

                            }

                        }



                    }

                    int i, j, k;

                    for (i = 0; i < 4; i++)

                    {

                        for (j = 2; j >= 0; j--)

                        {

                            k = j;

                            while (k + 1 <= 3 && a[i, k + 1] == 0)

                            {//上面的那个为空   

                                int temp;

                                temp = a[i, k];

                                a[i, k] = a[i, k + 1];

                                a[i, k + 1] = temp;

                                k++;

                            }

                        }

                    }

                }

                for (int i = 0; i < 4; i++)

                {

                    for (int j = 0; j < 4; j++)

                    {

                        if (a[i, j] == 2048)

                            ind = 1;

                    }



                }

                if (ind == 1)

                    break;

                Console.Clear();

                sc(a);

            }



            xs(a);

            Console.Read();

        }

    }

}

